package seis635.ui;

import seis635.design.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DateFormat;

public class CountdownUI{
	
	private DateFormat timerFormat = MainUI.getTimerFormat();
	private Countdown cd;
	private JLabel cdHourLabel;
	private JTextField cdTextFieldHour;
	private JLabel cdMinuteLabel;
	private JTextField cdTextFieldMin;
	private JLabel cdSecondLabel;
	private JTextField cdTextFieldSec;
	private JButton cdSetTimeButton;
	private JLabel cdLabel;
	private JButton cdStartButton;
	private JButton cdStopButton;
	private JPanel cdPanel;
	private JButton cdPauseButton;

	public JPanel getCDPanel(){
		return cdPanel;
	}
	public CountdownUI(Countdown c){
		
		this.cd = c;


		cdPanel = new JPanel();
		cdPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		cdHourLabel = new JLabel("Hour");
		cdPanel.add(cdHourLabel);

		cdTextFieldHour = new JTextField();
		cdPanel.add(cdTextFieldHour);
		cdTextFieldHour.setColumns(3);

		cdMinuteLabel = new JLabel("Minute");
		cdPanel.add(cdMinuteLabel);

		cdTextFieldMin = new JTextField();
		cdPanel.add(cdTextFieldMin);
		cdTextFieldMin.setColumns(3);

		cdSecondLabel = new JLabel("Second");
		cdPanel.add(cdSecondLabel);

		cdTextFieldSec = new JTextField();
		cdPanel.add(cdTextFieldSec);
		cdTextFieldSec.setColumns(3);

		cdSetTimeButton = new JButton("Set Time");
		cdSetTimeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int cdHour,cdMin,cdSec;
				seis635.design.Time time = new Time();
				cdHour = (cdTextFieldHour.getText().equals(""))? 23 : Integer.parseInt(cdTextFieldHour.getText());
				cdMin = (cdTextFieldMin.getText().equals(""))? 59 : Integer.parseInt(cdTextFieldMin.getText());
				cdSec = (cdTextFieldSec.getText().equals(""))? 59 : Integer.parseInt(cdTextFieldSec.getText());
				time.setHour(cdHour);
				time.setMinute(cdMin);
				time.setSecond(cdSec);
				cd.setTime(time);
				cdStartButton.setEnabled(true);
			}
		});
		cdPanel.add(cdSetTimeButton);

		cdStartButton = new JButton("Start");
		cdStartButton.setEnabled(false);
		cdStartButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cdStartButton.setText("Start");
				cdStopButton.setEnabled(true);
				cdPauseButton.setEnabled(true);
				cdStartButton.setEnabled(false);
				cdSetTimeButton.setEnabled(false);
				cd.start();
			}
		});
		cdPanel.add(cdStartButton);
		
		cdStopButton = new JButton("Stop");
		cdStopButton.setEnabled(false);
		cdStopButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cd.stop();
				cdStartButton.setEnabled(false);
				cdPauseButton.setEnabled(false);
				cdStartButton.setText("Start");
				cdSetTimeButton.setEnabled(true);

			}
		});
		
		cdPauseButton = new JButton("Pause");
		cdPauseButton.setEnabled(false);
		cdPauseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cdStartButton.setText("Resume");
				cdSetTimeButton.setEnabled(false);
				cdStartButton.setEnabled(true);
				cdPauseButton.setEnabled(false);
				cd.pause();
			}
		});
		cdPanel.add(cdPauseButton);
		cdPanel.add(cdStopButton);

		cdLabel = new JLabel(timerFormat.format(new java.util.Date(0)));
		cdPanel.add(cdLabel);

	}
	public void updateCDLabel(long time){
		this.cdLabel.setText(timerFormat.format(new java.util.Date(time)));
	}
}